// pages/explore/explore.js
import CommunityModule from '../../models/3.0/community'
import {category} from '../../models/explore'
import {video } from '../../models/explore_video'
import { attention} from '../../models/explore-attention'
import dateDiff from '../../util/dateDiff'
const newCategory =new category()
const givAttention =new attention()
const givvideo =new video()
Page({
  /**
   * Page initial data
   */
  data: {
    show:false,
    active: 1,
    list: [],
    showLoading:false,
    ownUserId: '',
    newVideo:[],
    newData:[],
    attentionData:[],
    count:10,
    page:1 ,
    boolean:false,
    videoCtx:'',
    isPraise:''
  },

  // 获取视频
  getvideo(){
    let params={
      count:this.data.count,
      page:this.data.page
    }
    givvideo.getvideo(params,res=>{
      console.log(res);
       this.setData({
        newVideo:res,
       })
    })
  }
,
  // 发现里面我加入的圈子
  getCategory(){
    newCategory.getCategory(res=>{
      console.log(res)
    this.setData({
      newData:res
      })
    })
  },
  // 发现里面我关注的动态
  getAttention(type){
    let params={
      count:this.data.count,
      page:this.data.page
    }
    givAttention.getAttention(params,res=>{
      console.log(res)
      this.setData({
        attentionData: type=='spinner'?[...this.data.attentionData, ...res] : res,
        showLoading: res.length >= 10,
      })
    })
  },
   
  // 调用刷新
  getNewlist(){
     this.getList()
  },

  getList(event) {
    CommunityModule.momentList({
      count: 50,
      page: 1
    }).then((res) => {
      console.log(res)
      res.forEach((item) => {
        item.topicName = item?.topicName?.split(',')
        item.images=item.images?.split(',')
        item.date = dateDiff(+new Date(item.createDate))
        if (item.content.length > 65) {
          item.expand = true
        } 
      })
      this.setData({
        list: res
      })
    })
  },
 
  play(){
    this.data.videoCtx.play()
  },

  like(e) {    
    if(e.target.dataset.like===false){
    CommunityModule.momentLike({
      id:  e.target.dataset.num,
      type: 0
    }).then(res=>{
      this.getvideo()
    })
  } else{
    CommunityModule.noMomentLike({
      id:  e.target.dataset.num,
      type: 0
    }).then(res=>{
      this.getvideo()
    })
} 
  },
  onShow() {
    this.getAttention()
    this.getTabBar().init()
    this.getList() //刷新的函数要调用才行
   this.getCategory()
   this.getvideo()
  },
  onLoad(){
    this.setData({
      ownUserId: wx.getStorageSync('userInfo').user_id,
      videoCtx:wx.createVideoContext('myVideo', this)
    })
  },

  onShareAppMessage() {
    return {
      title: '闲买足疗人',
      imageUrl: 'https://m.xianmaiyangsheng.com/xcxImg/image/zuliaoren/redpacketshare.png',
      path: `/pages/article/article?otherId=${wx.getStorageSync('userInfo').user_id}&time=${+new Date()}`
    }
  }
})
